home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / envoyempire / source / envoyserver.c < prev    next >
C/C++ Source or Header  |  1995-07-18  |  8KB  |  239 lines

  1. #include "stdarg.h"
  2. #include "guidefines.h"
  3. #include "defines.h"
  4. #include "struct.h"
  5. #include <envoy/nipc.h>
  6. #include <envoy/errors.h>
  7. #include <clib/nipc_protos.h>
  8. #include <pragmas/nipc_pragmas.h>
  9.  
  10. struct MyMsg 
  11. {
  12.   struct Message msg;
  13.   ULONG  Type;
  14.   ULONG  Length;            
  15.   UBYTE  *Data;
  16. };
  17.  
  18.  
  19.  
  20. extern struct WBStartup *WBenchMsg;
  21. struct Library *NIPCBase=NULL;
  22. ULONG bitnumber=0;
  23.  
  24. int processdata(struct Transaction *trans,struct MuxMsg *mmsg, struct MsgPort *in,struct Entity *myentity,struct Entity *sourceentity,struct MsgPort *,struct MsgPort *); 
  25. struct MsgPort *startserver (struct MyStartMsg *startupmsg);
  26. int setport(struct MuxMsg  *mmsg, struct MsgPort *in,struct MsgPort *muxport, struct MsgPort **dataport, struct MsgPort **serverport);
  27. int empire_talk(struct MsgPort *myport,struct Entity *myentity,struct MsgPort *serverport,struct MsgPort *dataport);
  28.  
  29.  
  30.   
  31. int empire_talk(struct MsgPort *in,struct Entity *myentity,struct MsgPort *serverport,struct MsgPort *dataport) {
  32.           
  33.   short quit = FALSE;
  34.   struct MuxMsg *mmsg;
  35.   struct Transaction *trans;
  36.   struct Entity *sourceentity=NULL;
  37.   
  38.   while(!quit)
  39.       {
  40.       while (mmsg = (struct MuxMsg *) GetMsg(in))
  41.       {
  42.         if (mmsg->msg.mn_ReplyPort == in)
  43.         {
  44.           if (mmsg->Data && mmsg->Length && (mmsg->Type == CSDATA))
  45.             FreeMem(mmsg->Data, mmsg->Length);
  46.           FreeMem(mmsg, SIZE(MuxMsg));
  47.         }
  48.         else if (mmsg->Type == CSDATA) processdata(NULL,(struct Message *) mmsg,in,myentity,sourceentity,serverport,dataport);
  49.         else if (mmsg->Type == CSKILL)
  50.         {
  51.           quit = TRUE;          
  52.           processdata(NULL,(struct Message *) mmsg,in,myentity,sourceentity,serverport,dataport);
  53.           Delay(30L);
  54.         }
  55.         else if (mmsg->Type == DIENOW)
  56.         {
  57.           quit = TRUE;
  58.           processdata(NULL,(struct Message *) mmsg,in,myentity,sourceentity,serverport,dataport);
  59.           Delay(30L);
  60.         }
  61.       }
  62.       while (trans = GetTransaction(myentity))
  63.       {
  64.         if (trans->trans_Type != TYPE_RESPONSE) sourceentity = trans->trans_SourceEntity;
  65.         else FreeTransaction(trans);
  66.         
  67.         if (trans->trans_Command == CSDATA) 
  68.            processdata(trans,NULL,in,myentity,sourceentity,serverport,dataport);
  69.         else if (trans->trans_Command == CSKILL)
  70.         { 
  71.           quit = TRUE;
  72.           processdata(trans,NULL,in,myentity,sourceentity,serverport,dataport);
  73.         }
  74.         else if (trans->trans_Command == DIENOW)
  75.         {
  76.           quit = TRUE;
  77.           processdata(trans,NULL,in,myentity,sourceentity,serverport,dataport);
  78.         }
  79.       }
  80.       if (!quit) Wait((1L << in->mp_SigBit) | (1L << bitnumber));
  81.  
  82.     } /** end while true do **/
  83.   return(1);
  84. }
  85.  
  86. long main(argc, argv)
  87. int argc;
  88. char *argv[];
  89. {
  90.   
  91.   char name[30];
  92.   struct MsgPort *serverport,*dataport,*myport,*parent;
  93.   struct Entity *myentity;
  94.   struct Library *SvcBase;
  95.   struct MyMsg *msg;
  96.   BOOL foo = TRUE;
  97.   
  98.   parent = FindPort("MUIEmpire_service.port");
  99.   if (!parent) exit(0);
  100.   
  101.   myport = CreateMsgPort();
  102.   
  103.   /* get entity name */
  104.   if (msg = (struct MyMsg *)AllocVec(sizeof(struct MyMsg),MEMF_CLEAR|MEMF_ANY)) {
  105.      msg->msg.mn_ReplyPort = myport;
  106.      msg->Length = 30;
  107.      msg->Type   = 1;
  108.      if (msg->Data = AllocVec(msg->Length,MEMF_CLEAR|MEMF_ANY)) {                      
  109.        PutMsg(parent, (struct Message *)msg); /* forward message to library */   
  110.      }
  111.      else {
  112.         FreeVec(msg);
  113.         DeletePort(myport);
  114.         exit(0);
  115.      }
  116.   }
  117.   
  118.   while (foo) {
  119.      while (msg = (struct MyMsg *)GetMsg(myport)) {
  120.         if (msg->Data) {
  121.            memcpy(name,msg->Data,msg->Length);
  122.            FreeVec(msg->Data);
  123.         }
  124.         FreeVec(msg);
  125.         foo = FALSE;
  126.      }
  127.      if (foo) WaitPort (myport);
  128.   }
  129.   foo = TRUE;
  130.     
  131.   if(NIPCBase = OpenLibrary("nipc.library",37L)) {
  132.     if(SvcBase = OpenLibrary("MUIEmpire.service", 0L)) {
  133.       if (myentity = CreateEntity(ENT_AllocSignal, &bitnumber,
  134.                                 ENT_Name,name,
  135.                                 ENT_Public, TRUE,
  136.                                 TAG_DONE)) {
  137.            /* inform the library the we are ready */
  138.            if (msg = (struct MyMsg *)AllocVec(sizeof(struct MyMsg),MEMF_CLEAR|MEMF_ANY)) {
  139.               msg->msg.mn_ReplyPort = myport;
  140.               msg->Length = 0L;
  141.               msg->Type   = 2;
  142.               PutMsg(parent, (struct Message *)msg); /* forward message to library */   
  143.               }
  144.            /* wait for the reply and free the message */
  145.            while (foo) {
  146.               while (msg = (struct MyMsg *)GetMsg(myport)) {
  147.                  if (msg->Data) FreeVec(msg->Data);
  148.                  FreeVec(msg);
  149.                  foo = FALSE;
  150.               }
  151.               if (foo) WaitPort (myport);
  152.            }
  153.            serverport = FindPort("EmpireServer");
  154.            dataport = FindPort("EmpireServerData");
  155.            if ((dataport && serverport) && myport) empire_talk(myport,myentity,serverport,dataport);
  156.            DeleteEntity(myentity);
  157.            printf("Quiting The Simpe Empire Envoy Server\n");
  158.        }
  159.        CloseLibrary(SvcBase);
  160.     }  
  161.     CloseLibrary(NIPCBase);
  162.   }
  163.   
  164.   DeletePort(myport);     
  165.   exit(0);
  166. }
  167.  
  168. int setport(struct MuxMsg  *mmsg, struct MsgPort *in,struct MsgPort *muxport, struct MsgPort **dataport, struct MsgPort **serverport)
  169.   {
  170.   
  171.   LONG *temp;
  172.   STRPTR name;
  173.  
  174.   if (mmsg->Data) temp = (LONG *) mmsg->Data;
  175.   else temp = NULL;
  176.   
  177.   if (temp) {
  178.     if (temp[0] == GUI_SEND) {
  179.       if (temp[1] == ID_PLAYGAME) {
  180.         if (name = AllocVec(50,MEMF_CLEAR|MEMF_ANY)) {
  181.           strmfe(name,"EmpireServer",&temp[2]);
  182.           printf("portname %s\n",name);
  183.           *serverport = FindPort(name);
  184.         }
  185.         if (*serverport) {
  186.            strmfe(name,"EmpireServerData",&temp[2]);
  187.            printf("dataportname %s\n",name);
  188.            *dataport = FindPort(name); 
  189.         }
  190.         FreeVec(name); 
  191.       }
  192.     }   
  193.   }
  194.   if (!(*dataport) || !(*serverport)) {
  195.      printf("failed to find ports!\n");
  196.      return(1);
  197.   }
  198.   return (0);
  199. }
  200.  
  201. int processdata (struct Transaction *trans,struct MuxMsg *mmsg, struct MsgPort *in,struct Entity *myentity,struct Entity *sourceentity, struct MsgPort *serverport, struct MsgPort *dataport) {
  202.   
  203.   LONG *temp;
  204.   struct MuxMsg *tempMuxMsg;
  205.   struct Transaction *temptrans;
  206.   
  207.   /* message from client for server */
  208.   if (trans) {
  209.      if (tempMuxMsg = (struct MuxMsg *)AllocMem(sizeof(struct MuxMsg),MEMF_CLEAR|MEMF_ANY)) {
  210.         tempMuxMsg->Type = trans->trans_Command;
  211.         tempMuxMsg->msg.mn_ReplyPort = in;
  212.         tempMuxMsg->Length = trans->trans_ReqDataActual;
  213.         if (temp = AllocMem(tempMuxMsg->Length,MEMF_CLEAR|MEMF_ANY)) {                     
  214.            memcpy(&temp[0],trans->trans_RequestData,trans->trans_ReqDataActual);
  215.            tempMuxMsg->Data = (UBYTE *)temp;
  216.            if ((temp[0] == GUI_SEND) || (temp[0] == GUI_PLANET)) 
  217.               PutMsg(serverport, (struct Message *) tempMuxMsg); /* forward message to server */
  218.            else PutMsg(dataport, (struct Message *) tempMuxMsg); /* forward message to server */
  219.         }
  220.         else PutMsg(serverport, (struct Message *) tempMuxMsg); /* forward message to server */   
  221.      }
  222.      ReplyTransaction(trans);
  223.   }
  224.   
  225.   /* message from server for client */
  226.   if (mmsg) {
  227.      if (temptrans = AllocTransaction(TRN_AllocReqBuffer,mmsg->Length,
  228.                                                 TAG_DONE)) {
  229.         temptrans->trans_Command       = mmsg->Type;
  230.         temptrans->trans_ReqDataActual = mmsg->Length;
  231.         if (mmsg->Length) memcpy(temptrans->trans_RequestData,mmsg->Data,mmsg->Length);
  232.         BeginTransaction(sourceentity,myentity,temptrans);    /* forward message to envoy */   
  233.      }
  234.      ReplyMsg((struct Message *) mmsg);
  235.   }
  236.   return (0); 
  237. }
  238.  
  239.